/*
 * Http.cpp
 *
 *  Created on: 2011/05/28
 *      Author: naoki
 */

#include "Stdafx.h"
#include "Net/src/ManagerImpl.h"
#include "Net/src/HttpImpl.h"

namespace NTEngine {
namespace Net {

const char Http::METHOD_GET[] = "GET";
const char Http::METHOD_POST[] = "POST";
const char Http::METHOD_PUT[] = "PUT";
const char Http::METHOD_DELETE[] = "DELETE";

Http Http::create( const char* url, const char* method, const char* body, const char** headers, int headerLength ) {
	Http ret;
	ret.mImpl = gManagerImpl->createHttp( url, method, body, headers, headerLength );
	return ret;
}

void Http::release() {
	if ( mImpl ) {
		mImpl->unref();
		gManagerImpl->destroyHttp( mImpl );
		mImpl = 0;
	}
}
int Http::responseCode() const {
	ASSERT( mImpl );
	return mImpl->responseCode();
}

const char* Http::responseBuffer() const {
	ASSERT( mImpl );
	return mImpl->responseBuffer();
}

int Http::responseSize() const {
	ASSERT( mImpl );
	return mImpl->responseSize();
}

#define TYPE Http
TYPE::TYPE()
: mImpl( 0 ) {
}

TYPE::~TYPE() {
	release();
}

TYPE::TYPE( const TYPE& a )
: mImpl( a.mImpl ) {
	if ( mImpl ) {
		mImpl->refer();
	}
}

TYPE& TYPE::operator=( const TYPE& a ) {
	release();
	mImpl = a.mImpl;
	if ( mImpl ) {
		mImpl->refer();
	}
	return *this;
}

bool TYPE::operator==( const TYPE& a ) const {
	return ( mImpl == a.mImpl );
}

bool TYPE::operator!=( const TYPE& a ) const {
	return ( mImpl != a.mImpl );
}

int TYPE::referenceCount() const {
	return ( mImpl ) ? mImpl->referenceCount() : 0;
}

TYPE::operator void*() const {
	return ( mImpl == 0 ) ? 0 : reinterpret_cast<void*>(~0);
}

#undef TYPE

} // namespace Net
} // namespace NTEngine
